﻿/*

/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
	Register	:	ลงทะเบียนUser
\												/


*/

$(function()
{
	//$("#tableregister").draggable();
	$('#reg_username').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '34'});
	$('#reg_password').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '34'});
	$('#reg_userId').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '34'});
	$('#reg_name').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '34'});
	$('#reg_year').button().css({'cursor': 'text','text-align': 'left','width': '180','height': '40'});
	$('#reg_submit').button();
	$('#reg_back').button();
	
});

function reg_register()
{
	if($('#reg_username').val()=='')
	{
		$('#dialog').html('Plase enter username');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#reg_password').val()=='')
	{
	$('#dialog').html('Plase enter the password');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#reg_userId').val()=='')
	{
	$('#dialog').html('Plase enter the student ID');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#reg_name').val()=='')
	{
	$('#dialog').html('Plase enter the name');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#reg_year').val()=='')
	{
	$('#dialog').html('Plase enter the year');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#reg_year').val() < 2012)
	{
	$('#dialog').html('Plase enter the year');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqregister.php',{ 'username': $('#reg_username').val(),'password': $('#reg_password').val(),'userId': $('#reg_userId').val(),'name': $('#reg_name').val(),'year': $('#reg_year').val()},function(data)
		{
			if(data == '1') {
				$('#dialog').html('Register Success');
				$('#dialog').dialog({'title':'Success'});
				$('#dialog').dialog('option','resizable',false);
				$.get('ui/login.php',function(data)
				{
					$('#main').html(data);
					$('#html_title').html('Register');
				});
			} else {
				$('#dialog').html('Username already used');
				$('#dialog').dialog({'title':'Success'});
				$('#dialog').dialog('option','resizable',false);
			}
		});
	}
	
}

function reg_back()
{
	$.get('ui/login.php',function(data)
	{
		$('#main').html(data);
		$('#html_title').html('Register');
	});
}